<?php

class AdminLoginController extends Controller
{
    public function defaultAction()
    {
        $this->login();
    }

    public function login()
    {
        if (isset($_POST['dologin']))
        {
            $username = InputFilter::fetch('username');
            $password = InputFilter::fetch('password');

            if (empty($username) || empty($password))
            {
                $this->_toOutput('error_msg', 'Korisničko ime ili zaporka nisu valjani.');
                $this->_display('admin', 'login');
                return;
            }

            $user_data = LoginModel::tryLogin($username, $password);
            if (empty($user_data))
            {
                $this->_toOutput('error_msg', 'Korisničko ime ili zaporka nisu valjani.');
                $this->_display('admin', 'login');
                return;
            }

            /*
            Debug::dump('login', '$user_data', $user_data);
            Debug::on();
            */

            $_SESSION['user_data'] = $user_data;

            switch ($user_data['user_level_name'])
            {
                case 'Administrator':
                    $controller = new AdminUserController();
                    $controller->listUsers();
                    return;
                case 'Portfolio owner':
                    $controller = new AdminPortfolioController();
                    $controller->listAlbums();
                    return;
            }
        }

        $this->_display('admin', 'login');
    }
}

?>